package net.tooeye.easyplus.easyplus.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;


import net.tooeye.easyplus.easyplus.R;
import net.tooeye.easyplus.easyplus.common.NetworkUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class SfctaActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonPost;
    private EditText editTextBar,editTextCount,editTextWeight,editTextHours, editTextTech,editTextEquipment;
    private  String host,token;
    private ImageView imageViewScan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sfcta);
        ActionBar bar = getSupportActionBar();
        if (bar != null){
            bar.hide();
        }
       init(); 
    }

    private void init() {
        Intent intent = getIntent();
        buttonPost = findViewById(R.id.buttonPost);
        buttonPost.setOnClickListener(this);
        imageViewScan = findViewById(R.id.imageViewScan);
        imageViewScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new IntentIntegrator(SfctaActivity.this)
                        .setPrompt("请对准一维或二维码")
                        .setCaptureActivity(ScanActivity.class)
                        .setCameraId(0)
                        .setBeepEnabled(true)
                        .initiateScan();
            }
        });
        editTextBar = findViewById(R.id.editbar);
        String bar;
        try{
            bar = intent.getStringExtra("bar");
        }
        catch (Exception ex ){
            bar = "";
        }
        editTextBar.setText(bar);
        editTextCount = findViewById(R.id.editCount);
        editTextCount.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        editTextTech = findViewById(R.id.editTech);
        editTextEquipment = findViewById(R.id.editEquipment);
        editTextHours =findViewById(R.id.editHours);
        editTextHours.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        editTextWeight = findViewById(R.id.editWeight);
        editTextWeight.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        SharedPreferences sp = getSharedPreferences("easy_plus", Context.MODE_PRIVATE);
        token =sp.getString("token","");
        host =sp.getString("host","");
        editTextEquipment.setText(sp.getString("EquipmentNo",""));
        editTextTech.setText(sp.getString("TechniqueNo",""));
    }

    private Map<String,String>  GetMap(){
        Map<String,String> params = new HashMap<>();
        params.put("token",token);
        params.put("barcode",editTextBar.getText().toString());
        params.put("Count",editTextCount.getText().toString());
        params.put("Weight",editTextWeight.getText().toString());
        params.put("ManHours",editTextHours.getText().toString());
        params.put("TechniqueNo", editTextTech.getText().toString());
        params.put("EquipmentNo",editTextEquipment.getText().toString());

        return  params;
    }

    @Override
    public void onClick(View v) {
        Map<String,String> params  = GetMap();
        String url = "http://" + host + "/Barcode/AddSfcBarcode";
        @SuppressLint("HandlerLeak") Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject((String)msg.obj);
                    Log.d("song", "handleMessage: "+ jsonObject.getString("Message"));
                    if (jsonObject.getBoolean("Status")) {
                        SharedPreferences sp = getSharedPreferences("easy_plus", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("TechniqueNo", editTextTech.getText().toString());
                        editor.putString("EquipmentNo", editTextEquipment.getText().toString());
                        editor.apply();
                        Toast.makeText(SfctaActivity.this, "提交完成", Toast.LENGTH_LONG).show();
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        NetworkUtil.getInstance().post(SfctaActivity.this,url,params,handler,1,"");
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(result != null) {
            if(result.getContents() == null) {

            } else {
                editTextBar.setText(result.getContents()+"");
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
